/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import mint.mvc.annotation.BaseMapping;
import mint.mvc.annotation.Mapping;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import pojo.User;
import util.HibernateUtil;

@BaseMapping("/user")
public class UserAction {

    /**
     * "/index" 和 "/"都可以访问到此方法
     *
     * @return
     */
    @Mapping(urls = {"/login"}, method = "post")
    public String login(String username, String password, HttpServletRequest hs) {
        Session ss = HibernateUtil.getSessionFactory().openSession();
        String hql = "from User where username = :n and password= :p";
        try {
            Query query = ss.createQuery(hql);
            query.setString("n", username);
            query.setString("p", password);
            List<User> list = query.list();

            if (list.size() > 0) {
                HttpSession se = hs.getSession();
                se.setAttribute("username", username);
                System.out.println("action.UserAction.login() session set succese");
                return "登入成功,<a href='/yc/index'>跳到管理首页</a>";
            }
        } catch (HibernateException e) {
            throw e;
        } finally {
            ss.close();
        }

        return "登入失败,请<a href='/yc/index'>重新登录</a>";
    }

    @Mapping(urls = {"/reset"}, method = "post")
    public String reset(String username, String password, String username_new, String password_new1, String password_new2) {
        Session ss = HibernateUtil.getSessionFactory().openSession();
        ss.beginTransaction();
        String hql = "from User where username = :n and password= :p";
        Query query = ss.createQuery(hql);
        query.setString("n", username);
        query.setString("p", password);
        List<User> list = query.list();

        if (list.size() > 0) {
//            HttpSession se=hs.getSession();
//            se.setAttribute("username", username);
//            System.out.println("action.UserAction.login() session set succese");
            User objUser = list.get(0);
            if (password_new1.equals(password_new2)) {
                objUser.setUsername(username_new);
                objUser.setPassword(password_new1);
                ss.update(objUser);
                ss.getTransaction().commit();
                ss.close();
                return "更改成功,<a href='../manager.jsp'>重新设置</a>";//+objUser.getId().toString();
            } else {
                ss.close();
                return "新密码2次输入不一致,<a href='../manager.jsp'>重新设置</a>";
            }
        }
        ss.close();
        return "旧用户名和旧密码不匹配,请<a href='/yc/index'>重新登录</a>";
    }

    @Mapping(urls = {"/logout"}, method = "get")
    public String logout(HttpServletRequest hs) {
        HttpSession se = hs.getSession();
        se.removeAttribute("username");
        return "登出成功,如有需要请<a href='/yc/index'>重新登录</a>";
    }
}
